package com.winit.openapi.oauth.manager.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiBizException;
import com.winit.openapi.oauth.dao.APISystemConfigDao;
import com.winit.openapi.oauth.entity.APISystemConfigEntity;
import com.winit.openapi.oauth.manager.APISystemConfigManager;
import com.winit.openapi.util.APISysConfigUtil;

@Service("apiSystemConfigManager")
public class APISystemConfigManagerImpl implements APISystemConfigManager {

    private Logger             logger = LoggerFactory.getLogger(APISystemConfigManagerImpl.class);

    @Resource
    private APISystemConfigDao apiSystemConfigDao;

    @Resource
    private APISysConfigUtil   apiSysConfigUtil;

    @Override
    public APISystemConfigEntity getEntityByKey(String configKey) {

        try {
            return apiSystemConfigDao.selectByKey(configKey);
        } catch (Exception e) {
            logger.error("系统配置查询异常,error:{}", e);
            throw new ApiBizException(ErrorCode.API_SYS_CONFIG_QUERY_ERROR);

        }

    }

    @Override
    public void update(APISystemConfigEntity entity) {
        entity.setUpdated(new Date());
        entity.setUpdatedby("SYSTEM");
        try {
            apiSystemConfigDao.updateConfig(entity);
            // 更新配置需要清除缓存数据
            logger.info("更新system配置项：" + JSON.toJSONString(entity));
        } catch (Exception e) {
            logger.error("系统配置更新异常,error:{}", e);
            throw new ApiBizException(ErrorCode.API_SYS_CONFIG_UPDATE_ERROR);

        }

    }

    @Override
    public void add(APISystemConfigEntity entity) {

        try {
            apiSystemConfigDao.insertSelective(entity);
        } catch (Exception e) {
            logger.error("系统配置添加异常,error:{}", e);
            throw new ApiBizException(ErrorCode.API_SYS_CONFIG_ADD_ERROR);

        }

    }

    @Override
    public boolean checkExists(String configKey) {
        try {
            return apiSystemConfigDao.selectByKey(configKey) != null;
        } catch (Exception e) {
            logger.error("系统配置查询异常,error:{}", e);
            throw new ApiBizException(ErrorCode.API_SYS_CONFIG_QUERY_ERROR);

        }
    }

    @Override
    public String getValueByKey(String configKey) {
        APISystemConfigEntity entity = apiSystemConfigDao.selectByKey(configKey);
        if (entity == null) {
            logger.warn("查询系统配置失败，不存在的配置项名称：" + configKey);

            return null;
        }

        try {
            return apiSystemConfigDao.selectByKey(configKey).getConfigValue();
        } catch (Exception e) {
            logger.error("系统配置查询异常,error:{}", e);
            throw new ApiBizException(ErrorCode.API_SYS_CONFIG_QUERY_ERROR);

        }
    }

    @Override
    public Map<String, APISystemConfigEntity> findAll() {
        try {
            List<APISystemConfigEntity> list = apiSystemConfigDao.findAll();
            if (CollectionUtils.isEmpty(list)) {
                return null;
            }
            Map<String, APISystemConfigEntity> map = new HashMap<String, APISystemConfigEntity>();
            for (APISystemConfigEntity entity : list) {
                map.put(entity.getConfigKey(), entity);
            }

            return map;

        } catch (Exception e) {
            logger.error("系统配置查询异常,error:{}", e);
            throw new ApiBizException(ErrorCode.API_SYS_CONFIG_QUERY_ERROR);

        }
    }

    @Override
    public void disableByKey(String configKey) {

        apiSystemConfigDao.disableByKey(configKey);

    }

    @Override
    public APISystemConfigEntity selectNonActiveEntityByKey(String configKey) {

        try {
            return apiSystemConfigDao.selectNonActiveEntityByKey(configKey);
        } catch (Exception e) {
            logger.error("系统配置查询异常,error:{}", e);
            throw new ApiBizException(ErrorCode.API_SYS_CONFIG_QUERY_ERROR);
        }
    }

}
